<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="css/common.css">
    <link rel="stylesheet" href="css/blog_detail.css">
    <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>

    <!-- 引入 editor.md 的依赖 -->
    <link rel="stylesheet" href="editor.md/css/editormd.min.css" />
    <script src="js/jquery.min.js"></script>
    <script src="editor.md/lib/marked.min.js"></script>
    <script src="editor.md/lib/prettify.min.js"></script>
    <script src="editor.md/editormd.js"></script>
    <script src="js/common.js"></script>
</head>
<body>
    <!--导航栏-->
    <div class="nav">
        <img src="pic/小组头像.jpg">
        <span>我的博客系统</span>
        <!-- 空白元素，用来占位 -->
        <div class="spacer"></div>

        <a href="blog_list.html">主页</a>
        <a href="blog_edit.html">写博客</a>
        <a href="logout">注销</a>
    </div>
    <div class="container">
        <div class="left">
            <!--用户信息区-->
            <div class="card">
                <img src="pic/小组头像.jpg">
                <h3>chuchu</h3>
                <a href="#">github地址</a>
                <div class="counter">
                    <span>文章</span>
                    <span>点赞</span>
                </div>
                <div class="counter">
                    <span id="articleNum"></span>
                    <span id="likeNum"></span>
                </div>
            </div>
        </div>
        <div class="right">
            <h3></h3>
            <span></span>
            <div id="content" style="opacity: 80%;"></div>
        </div>
    </div>

    <script>
        function getBlogDetail(){
            $.ajax({
                type: 'get',
                url: 'blog' + location.search, //要加上查询字符串，表示要访问博客详情页
                success: function(body){
                    //因为只有一个博客，所以不用appendChild去添加，直接给元素放内容就行
                    //标题
                    let h3 = document.querySelector('.right>h3');//要注意是right下的元素
                    h3.innerHTML = body.title;
                    //时间
                    let span = document.querySelector('.right>span');
                    span.innerHTML = body.postTime;
                    //内容
                    // let contentDiv = document.querySelector('.right>div');
                    // contentDiv.innerHTML = body.content;
                    editormd.markdownToHTML('content', {
                        markdown: body.content
                    });//第一个参数指要放哪(格式是id的),第二个参数指要放哪个内容
                },
                error: function(){
                    alert("获取博客详情页失败!");
                }
            })
        }
        getBlogDetail();//又忘了调用

        function getUserInfo(pagename){
            $.ajax({
                type: 'get',
                url: 'login',
                success: function(body){
                    if (body.userId && body.userId > 0){//存在并且大于0
                        console.log("登录成功,用户名为:" + body.username);
                        //获取作者的信息
                        getAuthorInfo(body);
                        if (pagename == "blog_list.html"){//博客详情页的名字需要另外改
                            changeUserName(body.username, body.articleNum, body.likeNum);
                        }
                    }else {
                        alert("当前未登录，请登录后访问!");
                        location.assign("blog_login.html");//重定向的方法
                    }
                }
            })
        }
        getUserInfo("blog_detail.html");

        function changeUserName(username, articleNum, likeNum){
            let h3 = document.querySelector('.card>h3');
            let article = document.querySelector('.counter>#articleNum');
            let like = document.querySelector('.counter>#likeNum');
            h3.innerHTML = username;
            article.innerHTML = articleNum;
            like.innerHTML = likeNum;
        }

        function getAuthorInfo(user){
            $.ajax({
                type: 'get',
                url: 'author' + location.search,
                success: function(body){
                    changeUserName(body.username, body.articleNum, body.likeNum);
                    if (body.username == user.username){
                        //是本人查看，就创造一个删除按钮
                        let navDiv = document.querySelector('.nav');
                        let a = document.createElement('a');
                        a.innerHTML = '删除';
                        a.href = 'delete' + location.search;
                        navDiv.appendChild(a);
                    }else {
                        console.log("获取作者信息失败！");
                    }
                }
            })
        }
        getAuthorInfo();
    </script>
</body>
</html>